package com.etooling.pdm.controller;

import com.etooling.framework.jpa.JpaQueryBuilder;
import com.etooling.framework.jpa.Sorts;
import com.etooling.framework.jpa.Specifications;
import com.etooling.framework.svo.ReplyObj;
import com.etooling.framework.utils.WebUtils;
import com.etooling.pdm.entity.GoodsPropSetting;
import com.etooling.pdm.repository.GoodsPropSettingRepository;
import com.etooling.pdm.service.GoodsPropSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author dan
 * @date 2023/12/28 11:49
 */
@RequestMapping(value = "/admin/pdm/goodsPropSetting")
@Controller
@EnableAutoConfiguration
public class GoodsPropSettingController {

    @Autowired
    GoodsPropSettingRepository goodsPropSettingRepository;
    @Autowired
    GoodsPropSettingService goodsPropSettingService;


    @ResponseBody
    @RequestMapping("pageQueryPropList")
    ReplyObj pageQueryPropList(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "goodsTypeId") String goodsTypeId) {
        if (WebUtils.checkStr(goodsTypeId)) {
            JpaQueryBuilder<GoodsPropSetting> pb = Specifications.and();
            pb.eq("goodsType.id", goodsTypeId);
            pb.eq("status", GoodsPropSetting.STATUS_RUNNING);
            Sort sort = Sorts.builder().asc("seq").build();
            return WebUtils.flushSuccess(goodsPropSettingRepository.findAll(pb.build(), sort));
        } else {
            return WebUtils.flushFailure("没有选择对应产品");
        }
    }


    @ResponseBody
    @RequestMapping("getPropList")
    ReplyObj getPropList(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "goodsTypeId") String goodsTypeId,
                         @RequestParam(defaultValue = "", required = false, value = "goodsId") String goodsId) {
        if (WebUtils.checkStr(goodsTypeId)) {
            List<Map<String, Object>> propList = goodsPropSettingRepository.getPropList(goodsTypeId, goodsId);
            return WebUtils.flushSuccess(propList);
        } else {
            return WebUtils.flushFailure("没有选择对应产品");
        }
    }


    /**
     * 新增
     */
    @ResponseBody
    @RequestMapping("add")
    ReplyObj add(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "json") String json) {
        return goodsPropSettingService.add(request, json);
    }


    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("update")
    ReplyObj update(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "json") String json) {
        return goodsPropSettingService.update(request, json);
    }


    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("del")
    ReplyObj del(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "id") String id) {
        try {
            goodsPropSettingRepository.deleteById(id);
            return WebUtils.flushSuccess("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure("无法删除，此配置已经用于“产品/型号”的创建");
        }
    }

}
